using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Drawing2D;

namespace SkinableFormsLib
{
	public class LWPictureBox : PictureBox, ILWControl
	{


		internal Bitmap _lwBitmap = null;
		internal Graphics _lwGraphics;
		internal Rectangle _lwRectangle;

		protected override void OnResize(System.EventArgs e)
		{

			if (_lwBitmap == null || _lwBitmap.Width != Width || _lwBitmap.Height != Height)
			{

				if (Width != 0 && Height != 0)
				{
					this.DisposeInternal();
					CreateBitmap();
				}
			}
		}
		private void CreateBitmap()
		{
			if (Width != 0 && Height != 0)
			{
				_lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
				_lwGraphics = Graphics.FromImage(_lwBitmap);
				_lwGraphics.SmoothingMode = SmoothingMode.HighQuality;
				_lwGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
				_lwGraphics.CompositingQuality = CompositingQuality.HighQuality;
				_lwRectangle = new Rectangle(0, 0, _lwBitmap.Width, _lwBitmap.Height);
				
			}
		}


		internal virtual void DisposeInternal()
		{
			if (_lwBitmap != null)
				_lwBitmap.Dispose();
			if (_lwGraphics != null)
				_lwGraphics.Dispose();

		}
		protected override void OnPaintBackground(PaintEventArgs pevent)
		{
			base.OnPaintBackground(pevent);
		}



		protected override void OnPaint(PaintEventArgs pe)
		{

			//ensure background redraw
			CreateBitmap();


			PaintEventArgs e = new PaintEventArgs(_lwGraphics, pe.ClipRectangle);
			base.OnPaint(e);
		}


		







		public bool IsImageBigger
		{

			get
			{
				if (this.Image != null)
				{
					return this.Image.Size.Width > Width || this.Image.Size.Height > Height;
				}
				return false;
			}
		}





		#region ILWControl Members

		[Browsable(false)]
		Bitmap SkinableFormsLib.ILWControl.LWBitmap
		{
			get
			{
				return _lwBitmap;

			}
		}

		internal bool _lwTransparent = true;
		public bool LWTransparent
		{
			get
			{
				return _lwTransparent;
			}
			set
			{
				_lwTransparent = value;
			}
		}


		public Color LWTransparentColor
		{
			get
			{
				return Color.Transparent;
			}
			set
			{

			}
		}

		[Browsable(false)]
		public bool LWTransparency
		{
			get
			{
				return true;

			}
		}

		public Color LWDesiredBackColor
		{
			get
			{
				return Color.Transparent;
			}
			set
			{

			}
		}

		public bool LWDragForm
		{
			get
			{
				return true;
			}
			set
			{
				
			}
		}

		#endregion
	}
}
